package com.mediacloud.app.newsmodule.activity.microlive.x

import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager
import com.androidkun.xtablayout.XTabLayout
import com.mediacloud.app.model.utils.DefaultBgUtil
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.MicroLiveTableAdaptor
import com.mediacloud.app.newsmodule.activity.microlive.MicroLiveWebViewFragment

/**
 * 统一处理已选Tab 和排序相关的东西
 */
abstract class BaseMicroLiveTab(activity: FragmentActivity) : BaseMicroLive(activity), CompoundButton.OnCheckedChangeListener {
    var microLiveTableAdaptor: MicroLiveTableAdaptor = MicroLiveTableAdaptor(activity.supportFragmentManager, fragmentList())
    var microTableLayout: XTabLayout = findViewById(R.id.microTableLayout)
    var microOder: CheckBox = findViewById(R.id.microOder)
    var microViewPager: ViewPager = findViewById(R.id.microViewPager)

    //图文直播TAB状态下 这东西肯定是有的
    var microLiveWebViewFragment: MicroLiveWebViewFragment = MicroLiveWebViewFragment()

    init {
        titleList().forEach {
            microLiveTableAdaptor.titile.put(microLiveTableAdaptor.titile.size(), it)
        }
        microViewPager.offscreenPageLimit = microLiveTableAdaptor.list.size
        microViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                if (position == microViewPager.childCount - 1) {
                    microOder.visibility = View.VISIBLE
                } else {
                    microOder.visibility = View.GONE
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        microViewPager.adapter = microLiveTableAdaptor
        microTableLayout.setupWithViewPager(microViewPager)
        microTableLayout.setTabTextColors(activity.resources.getColor(R.color.appfac_microlive_jinan), DefaultBgUtil.getTintColor(activity))
        microTableLayout.setSelectedTabIndicatorColor(DefaultBgUtil.getTintColor(activity))
        microOder.setOnCheckedChangeListener(this)
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        microLiveWebViewFragment.callOrderJS(if (isChecked) {
            0
        } else {
            1
        })
    }

    abstract fun fragmentList(): ArrayList<Fragment>
    abstract fun titleList(): MutableList<String>
}